<?php
/*
	This file is part of PGallery; a FrogCMS plugin.

	PGallery is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	PGallery is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with PGallery.  If not, see <http://www.gnu.org/licenses/>.
*/


class LibMagick
{
	
	
	// Path to Imagick
	private $path;
	// Paths array to all Imagick binaries
	private $paths;
	// Upload path
	private $upload_path;
	// Separator
	private $sep;
	// Last message text that can be access by parent object
	public $msg;
	
	
	
	
	public function magick_setPath($im_path){
		// Set ImageMagick executable path from paramter
		$this->path = $im_path;
		$this->sep = (preg_match('/\\\/', __FILE__)) ? '\\' : '/';
		$this->paths['convert'] = $this->path . $this->sep . 'convert' . (($this->sep == '/') ? '' : '.exe');
		#die(print_r($this->paths));
	}
	
	
	
	
	/**
	 * Set the final destination upload dir (the dir that final images will be written to)
	 *
	 * @param	string		upload_path		Full path to image directory
	 */
	public function setUploadPath($upload_path){
		$this->upload_path = $upload_path;
	}
	
	
	
	
	/**
	 * Test if convert exists and run a test
	 */
	public function test(){
		
		if(!file_exists($this->paths['convert'])){
			$this->msg = "Path to ImageMagick's convert ({$this->paths['convert']}) is not valid.";
			return FALSE;
		}
		
		$dest = $this->upload_path.$this->sep.uniqid().'.jpg';
		$cmd = $this->paths['convert'] . ' logo: ' . $dest;
		
		exec($cmd, $out, $ret);
		
		if($ret > 0 && !file_exists($dest)){
			$this->msg = 'Failed to write test image to ' . $dest . '. Please check file permissions. (ERR: ' . implode("\n", $out) . ')';
			return FALSE;
		}
		
		@unlink($dest);
		
	}
	
	
	
	
	/**
	 * Resize an image
	 *
	 * @param	string	filename		Full path to source file
	 * @param	string	wantedsize		Desired length of longest side
	 * @param	string	newname			New filename to write to
	 * @param	bool	square			Crop to make the image square
	 */
	public function resize($filename, $wantedsize, $newname, $square = FALSE){
		
		// Get dimensions & info about the image
		$imagesize = getimagesize($filename);
		// Make a full path to the new file
		$newname = $this->upload_path.$this->sep.$newname;
		
		$orig_width = $imagesize[0];
		$orig_height = $imagesize[1];
		
		
		// Work out the dimensions of the image based on longest side, or set both to same if equal
		if ($orig_width > $orig_height){
			$width = $wantedsize;
			$height = ceil($orig_height * ($wantedsize / $orig_width));
			$sq = 'x' . $wantedsize;
		} elseif($orig_width < $orig_height){
			$width = ceil($orig_width * ($wantedsize / $orig_height));
			$height = $wantedsize;
			$sq = $wantedsize . 'x';
		} elseif($imagesize[0] == $imagesize[1]){
			$width = $wantedsize;
			$height = $wantedsize;
		}
		
		// Need to run slightly different command if we need to make it square
		if($square == FALSE){
			$cmd = '%s -quality 100 -resize %dx%d "%s" "%s"';
			$cmd = sprintf($cmd, $this->paths['convert'], $width, $height, $filename, $newname);
		} else {
			$cmd = '%s -quality 100 -resize %2$s -gravity center -crop %3$dx%3$d+0+0 +repage "%4$s" "%5$s"';	#-gravity center -crop 100x100+0+0
			$cmd = sprintf($cmd, $this->paths['convert'], $sq, $wantedsize, $filename, $newname);
		}
		
		// Run it!
		@exec($cmd, $out, $ret);
		
		// Check if an error occured
		if($ret > 0 && !file_exists($newname)){
			$this->msg =  "Failed when running command '" . $cmd . "': " . implode("\n", $out);
			return FALSE;
		}
		
		return TRUE;
		
	}
	
	
	
	
}